﻿@using System.Diagnostics.CodeAnalysis
@using System.Globalization;

@namespace Gurux.DLMS.AMI.Components
<div class="row">
    <div class="input-group col-md-4">
        <input class="form-control py-2 border-right-0 border"
               type="search"
               value="@Value"
               onsearch="@(() => OnSearch())"
               placeholder="@Placeholder"
               oninput="@((ChangeEventArgs __e) => Value = Convert.ToString(__e.Value))" />
        <span class="input-group-append">
            <button class="btn btn-outline-secondary border-left-0 border" type="button"
                    onclick="@(() => OnSearch())">
                <i class="oi oi-magnifying-glass"></i>
            </button>
        </span>
    </div>
</div>
@code {
    public delegate Task SearchDelegate(string? value);


    [Parameter]
    public string? CssClass { get; set; }

    [Parameter]
    public string? Placeholder { get; set; }

    [Parameter(CaptureUnmatchedValues = true)]
    public IReadOnlyDictionary<string, object>? AdditionalAttributes { get; set; }

    public string? Value { get; set; }

    /// <summary>
    /// Action that is executed when the user press the search button.
    /// </summary>
    [Parameter]
    public Action<string?>? OnClick { get; set; }

    /// <summary>
    /// Action that is executed when the user press the search button.
    /// </summary>
    [Parameter]
    public SearchDelegate? OnClickAsync { get; set; }

    private void OnSearch()
    {
        if (OnClick != null)
        {
            OnClick(Value);
        }
        else if (OnClickAsync != null)
        {
            OnClickAsync(Value);
        }
    }
}